<?php
	define('ROOT_PATH', strtr(dirname( __FILE__ ) ."/",array('\\'=>'/')));

	require_once ROOT_PATH."core/Init.php";
	$return = 'false,Unknown Error';
	
	if($_SERVER['HTTP_REFERER'] == 'enbac.com')
	{
		$imageFileName 		= $_REQUEST["imageFileName"];
		$desImageName 		= $_REQUEST["desImageName"];
		$imageFileFolder	= $_REQUEST["imageFileFolder"];
		$cropX1				= $_REQUEST["x1"];
		$cropY1				= $_REQUEST["y1"];
		$cropWidth			= $_REQUEST["w"];
		$cropHeight			= $_REQUEST["h"];
		$displayWidth		= $_REQUEST["displayWidth"];
		$displayHeight		= $_REQUEST["displayHeight"];
		
		$imageName 			= $imageFileFolder . $imageFileName;
	
		list($imagewidth, $imageheight, $imageType) = getimagesize($imageName);
	
		if($imagewidth){
			//if((int)ini_get("memory_limit")<64){@ini_set("memory_limit","64M");}
			
			// Lấy cỡ ảnh mới
			$scaleW 			= $imagewidth / $displayWidth;
			$scaleH				= $imageheight / $displayHeight;
			$w 					= (int)($cropWidth * $scaleW);
			$h 					= (int)($cropHeight * $scaleH);
			$x1 				= (int)($cropX1 * $scaleW);
			$y1 				= (int)($cropY1 * $scaleH);
			
			$imageType = image_type_to_mime_type($imageType);
			$cropName = $imageFileFolder . $desImageName;
			
			if($imageType)
			{
				switch($imageType) {
					case "image/gif":
						$source=imagecreatefromgif($imageName); 
						break;
					case "image/pjpeg":
					case "image/jpeg":
					case "image/jpg":
						$source=imagecreatefromjpeg($imageName); 
						break;
					case "image/png":
					case "image/x-png":
						$source=imagecreatefrompng($imageName); 
						break;
				}
				
				if($source)
				{		
					$newImage = imagecreatetruecolor($w,$h);
					if($newImage)
					{	
						if(imagecopyresampled($newImage,$source,0,0,$x1,$y1,$w,$h,$w,$h))
						{
							if(USE_FTP_IMAGE)
							{
								$tempName 	= IMG_CACHE_PATH . $desImageName;
								
								switch($imageType) {
									case "image/gif":
										$createdImage = imagegif($newImage,$tempName); 
										break;
									case "image/pjpeg":
									case "image/jpeg":
									case "image/jpg":
										$createdImage = imagejpeg($newImage,$tempName,99); 
										break;
									case "image/png":
									case "image/x-png":
										$createdImage = imagepng($newImage,$tempName);  
										break;
								}
								
								if($createdImage){
									if(ImageLib::ftp_check_dir('up_new',true)){
										if(ImageLib::ftp_image_put_file($cropName,$tempName)){
											unlink($tempName);
											ImageLib::ftp_image_delete_file($imageName);
											$return = 'true';
										}
										else{
											$return = 'false,Copy file failed, permission denied';
										}
									}
									else{
										$return = 'false,Destination folder not found';
									}
								}
								else{
									$return = 'false,Refine image failed';
								}
							}
							else
							{
								
								switch($imageType) {
									case "image/gif":
										$createdImage = imagegif($newImage,$cropName); 
										break;
									case "image/pjpeg":
									case "image/jpeg":
									case "image/jpg":
										$createdImage = imagejpeg($newImage,$cropName,99); 
										break;
									case "image/png":
									case "image/x-png":
										$createdImage = imagepng($newImage,$cropName);  
										break;
								}
								if($createdImage)
								{
									if(chmod($cropName, 0777))
									{
										unlink($imageName);
										$return = 'true';
									}
									else
									{
										$return = 'false,Set permission failed';
									}
								}
								else
								{
									$gd = gd_info();

									while( list( $k, $v ) = each( $gd ) ) { echo "$k: $v<br />"; }
									$return = 'false,Refine image failed: ' . $tempName;
								}
							}
						}
						else
						{
							$return = 'false,Crop image failed';
						}
					}
					else
					{
						$return = 'false,Verify image color failed';
					}
				}
				else
				{
					$return = 'false,Get image source failed';
				}
			}
			else
			{
				$return = 'false,Image type is not supported';
			}
		}
		else
		{
			$return = 'false,The source file is not an image';
		}
	}
	else
	{
		$return = 'false,The request come from outside is not acceptable';
	}
	echo $return;
	exit();
?>